using System;
using System.Collections.Generic;
using System.Text;

namespace Acms.DataControls
{
    [System.ComponentModel.Designer(typeof(PostDataSourceDesigner))]
    [System.Web.UI.ToolboxData("<{0}:PostDataSource runat=server/>")]
    public class PostDataSource : GenericDataSource<Bll.Post>
    {
        public enum Fields
        {
            Id,
            Name,
            DisplayName,
            SiteId,
            ParentId,
            TemplateId,
            Summary,
            Sequence,
            ConcurencyId
        }

        protected override Acms.Bll.Post PopulateData(UpdateAction action, System.Collections.IDictionary keys, System.Collections.IDictionary values, System.Collections.IDictionary oldValues)
        {
            Bll.Post tmp = null;
            byte[] concurencyId = null;
            if (action == UpdateAction.Insert)
            {
                tmp = Bll.Post.CreateInstance();
            }
            else
            {
                int id = GetValue<int>(Fields.Id, keys);
                concurencyId = GetValue<byte[]>(Fields.ConcurencyId, keys);
                tmp = Bll.Post.GetById(id);
            }

            if (tmp != null)
            {
                tmp.CheckConcurent(concurencyId);

                if (RequiresUpdate<int>(Fields.SiteId, values, oldValues))
                {
                    tmp.SiteId = GetValue<int>(Fields.SiteId, values);
                }
                if (RequiresUpdate<string>(Fields.Name, values, oldValues))
                {
                    tmp.Name = GetValue<string>(Fields.Name, values);
                }
                if (RequiresUpdate<string>(Fields.DisplayName, values, oldValues))
                {
                    tmp.DisplayName = GetValue<string>(Fields.DisplayName, values);
                }
                if (RequiresUpdate<int>(Fields.ParentId, values, oldValues))
                {
                    tmp.ParentId = GetValue<int?>(Fields.ParentId, values);
                }
                if (RequiresUpdate<int>(Fields.TemplateId, values, oldValues))
                {
                    tmp.TemplateId = GetValue<int>(Fields.TemplateId, values);
                }
                if (RequiresUpdate<string>(Fields.Summary, values, oldValues))
                {
                    tmp.Summary = GetValue<string>(Fields.Summary, values);
                } 
                if (RequiresUpdate<int>(Fields.Sequence, values, oldValues))
                {
                    tmp.Sequence = GetValue<int>(Fields.Sequence, values);
                }
            }

            return tmp;

        }

        protected override int SaveChanges(Acms.Bll.Post item)
        {
            item.Update();
            return 1;
        }

        protected override int Delete(Acms.Bll.Post item)
        {
            item.Delete();
            return 1;
        }

        protected override void AddInsertedKey(Acms.Bll.Post item, System.Collections.IDictionary values)
        {
            values[Fields.Id.ToString()] = item.Id;
        }
    }


    public class PostDataSourceDesigner : GenericDataSourceDesigner<Bll.Post>
    {
        protected override Acms.Bll.Post GetDesignTimeItem(int index)
        {
            Random rnd = new Random(index);
            string[] names = new string[] { "Home", "My Photos", "Section 1", "User Details", "Issue 1"};
            Bll.Post tmp = Acms.Bll.Post.CreateInstance();
            tmp.DisplayName = names[rnd.Next(0, names.Length)];
            tmp.Name = tmp.DisplayName.Replace(" ", "");
            tmp.SiteId = 1;
            tmp.ParentId = 2;
            tmp.TemplateId = 1;
            return tmp;
        }
    }
}
